package com.sqfw.project.zj.controller;

import com.sqfw.common.utils.poi.ExcelExp;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.common.utils.poi.ExcelUtilManySheet;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.wpReport.domain.WpInfoEvaluate;
import com.sqfw.project.zj.domain.*;
import com.sqfw.project.zj.service.IZjRecordScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 录音质检评分Controller
 *
 * @author ruoyi
 * @date 2023-07-17
 */
@RestController
@RequestMapping("/zj/recordScore")
public class ZjRecordScoreController extends BaseController
{
    @Autowired
    private IZjRecordScoreService zjRecordScoreService;

    /**
     * 查询录音质检评分列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ZjRecordScore zjRecordScore)
    {
        startPage();
        List<ZjRecordScore> list = zjRecordScoreService.selectZjRecordScoreList(zjRecordScore);
        return getDataTable(list);
    }

    /**
     * 查询话务员列表
     */
    @PreAuthorize("@ss.hasPermi('recordScore:seats:list')")
    @GetMapping("/seats/list")
    public TableDataInfo getSeatList()
    {
        List<SysUser> list = zjRecordScoreService.getSeatList();
        return getDataTable(list);
    }

    /**
     * 查询质检组绩效表
     */
    @PreAuthorize("@ss.hasPermi('recordScore:performance')")
    @GetMapping("/performance")
    public TableDataInfo getPerformanceList(ZjPerformance zjPerformance)
    {
        startPage();
        TableDataInfo list = zjRecordScoreService.getPerformanceList(zjPerformance);
        return list;
    }

    /**
     * 导出质检组绩效表
     */
    @PreAuthorize("@ss.hasPermi('recordScore:performance:export')")
    @PostMapping("/performance/export")
    public void exportPerformance(HttpServletResponse response, ZjPerformance zjPerformance)
    {
        TableDataInfo tableDataInfo = zjRecordScoreService.getPerformanceList(zjPerformance);
        List<ZjPerformance> list = (List<ZjPerformance>) tableDataInfo.getRows();
        ExcelUtil<ZjPerformance> util = new ExcelUtil<>(ZjPerformance.class);
        util.exportExcel(response, list, "质检组绩效表");
    }

    /**
     * 查询质检组绩效统计表
     */
    @PreAuthorize("@ss.hasPermi('recordScore:performance:stat')")
    @GetMapping("/performance/stat")
    public TableDataInfo getPerformanceStatList(ZjPerformanceStat zjPerformanceStat)
    {
        startPage();
        TableDataInfo statList = zjRecordScoreService.getPerformanceStatList(zjPerformanceStat);
        return statList;
    }

    /**
     * 导出-质检组绩效统计表
     */
    @PostMapping("/performance/stat/export")
    public void exportPerformanceStat(HttpServletResponse response, ZjPerformanceStat zjPerformanceStat)
    {
        TableDataInfo tableDataInfo = zjRecordScoreService.getPerformanceStatList(zjPerformanceStat);
        List<ZjPerformanceStat> list = (List<ZjPerformanceStat>) tableDataInfo.getRows();

        ExcelUtil<ZjPerformanceStat> util = new ExcelUtil<>(ZjPerformanceStat.class);
        util.exportExcel(response, list, "质检组绩效统计表");
    }

    /**
     * 自办件统计表
     */
    @PreAuthorize("@ss.hasPermi('recordScore:selfhandle:stat')")
    @GetMapping("/selfhandle/stat")
    public TableDataInfo getSelfHandleStatList(ZjSelfHandleStat zjSelfHandleStat)
    {
        startPage();
        TableDataInfo tableDataInfo = zjRecordScoreService.getSelfHandleStatList(zjSelfHandleStat);
        return tableDataInfo;
    }

    /**
     * 自办件统计表
     */
    @PreAuthorize("@ss.hasPermi('selfhandle:stat:export')")
    @PostMapping("/selfhandle/stat/export")
    public void exportSelfHandleStat(HttpServletResponse response, ZjSelfHandleStat zjSelfHandleStat)
    {
        TableDataInfo tableDataInfo = zjRecordScoreService.getSelfHandleStatList(zjSelfHandleStat);
        List<ZjSelfHandleStat> list = (List<ZjSelfHandleStat>) tableDataInfo.getRows();

        //总计

        ZjSelfHandleStat stat = new ZjSelfHandleStat();

        int returnA = list.stream()
                .map(ZjSelfHandleStat::getReturnA)
                .reduce(Integer::sum)
                .orElse(0);
        stat.setReturnCountA(String.valueOf(returnA));

        int returnB = list.stream()
                .map(ZjSelfHandleStat::getReturnB)
                .reduce(Integer::sum)
                .orElse(0);
        stat.setReturnCountB(String.valueOf(returnB));

        int returnC = list.stream()
                .map(ZjSelfHandleStat::getReturnC)
                .reduce(Integer::sum)
                .orElse(0);
        stat.setReturnCountC(String.valueOf(returnC));

        int returnD = list.stream()
                .map(ZjSelfHandleStat::getReturnD)
                .reduce(Integer::sum)
                .orElse(0);
        stat.setReturnCountD(String.valueOf(returnD));
        //退单总数
        int returnCountSum = list.stream()
                .map(ZjSelfHandleStat::getReturnCountSum)
                .reduce(Integer::sum)
                .orElse(0);
        stat.setReturnCountSum(returnCountSum);

        //12345工单总量
        int wpSumCount = list.stream()
                .map(ZjSelfHandleStat::getWpSumCount)
                .reduce(Integer::sum)
                .orElse(0);
        stat.setWpSumCount(wpSumCount);

        //12345自办件数 wpSelfSumCount

        int wpSelfSumCount = list.stream()
                .map(ZjSelfHandleStat::getWpSelfSumCount)
                .reduce(Integer::sum)
                .orElse(0);
        stat.setWpSelfSumCount(wpSelfSumCount);
        //自办件退单占比
        String returnPoint = getReturnPoint(wpSelfSumCount, returnCountSum);
        stat.setSelfPerRet(returnPoint + "%");

        stat.setAgentName("总计");
        list.add(stat);
        ExcelUtil<ZjSelfHandleStat> util = new ExcelUtil<>(ZjSelfHandleStat.class);
        util.exportExcel(response, list, "自办件统计表");
    }


    /**
     * 查询服务监听评分列表
     */
    @PreAuthorize("@ss.hasPermi('serviceMonitor')")
    @GetMapping("/serviceMonitor")
    public TableDataInfo list(ZjServiceMoniScoreExcelVo zjServiceMoniScoreExcelVo)
    {
        startPage();
        return zjRecordScoreService.selectZjServiceMoniScoreExcelVoList(zjServiceMoniScoreExcelVo);
    }

    /**
     * 导出服务监听评分列表
     */
    @PreAuthorize("@ss.hasPermi('serviceMonitor:export')")
    @PostMapping("/serviceMonitor/export")
    public void exportZjServiceMoniScoreExcelVo(HttpServletResponse response, ZjServiceMoniScoreExcelVo zjServiceMoniScoreExcelVo) throws IOException {
        TableDataInfo tableDataInfo = zjRecordScoreService.selectZjServiceMoniScoreExcelVoList(zjServiceMoniScoreExcelVo);
        List<ZjServiceMoniScoreExcelVo> list = (List<ZjServiceMoniScoreExcelVo>) tableDataInfo.getRows();
        Map<String, List<ZjServiceMoniScoreExcelVo>> excelVoMap = list.stream().collect(Collectors.groupingBy(ZjServiceMoniScoreExcelVo::getAgentName));
        ExcelUtil<ZjServiceMoniScoreExcelVo> util = new ExcelUtil<>(ZjServiceMoniScoreExcelVo.class);
        List<ExcelExp> mySheet = new ArrayList<>();
        for (Map.Entry<String, List<ZjServiceMoniScoreExcelVo>> entry : excelVoMap.entrySet()) {
            List<ZjServiceMoniScoreExcelVo> vos = entry.getValue();
            //根据日期进行降序排序
            List<ZjServiceMoniScoreExcelVo> voList = vos.stream().sorted(Comparator.comparing(ZjServiceMoniScoreExcelVo::getWpDate).reversed()).collect(Collectors.toList());
            ExcelExp avi = new ExcelExp(entry.getKey(), voList, ZjServiceMoniScoreExcelVo.class);
            mySheet.add(avi);
        }
        ExcelUtilManySheet<List<ZjServiceMoniScoreExcelVo>> util2 = new ExcelUtilManySheet<>(mySheet);
        util2.exportExcelManySheet(response, mySheet);
        util.exportExcel(response, list, "服务监听评分列表");
    }

    /**
     * 导出录音质检评分列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZjRecordScore zjRecordScore)
    {
        List<ZjRecordScore> list = zjRecordScoreService.selectZjRecordScoreList(zjRecordScore);
        ExcelUtil<ZjRecordScore> util = new ExcelUtil<ZjRecordScore>(ZjRecordScore.class);
        util.exportExcel(response, list, "录音质检评分数据");
    }

    /**
     * 获取录音质检评分详细信息
     */
    @GetMapping(value = "/{scid}")
    public AjaxResult getInfo(@PathVariable("scid") Long scid)
    {
        return success(zjRecordScoreService.selectZjRecordScoreByScid(scid));
    }

    /**
     * 新增录音质检评分
     */
    @PostMapping
    public AjaxResult add(@RequestBody ZjRecordScore zjRecordScore)
    {
        return toAjax(zjRecordScoreService.insertZjRecordScore(zjRecordScore));
    }

    /**
     * 修改录音质检评分
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ZjRecordScore zjRecordScore)
    {
        return toAjax(zjRecordScoreService.updateZjRecordScore(zjRecordScore));
    }

    /**
     * 删除录音质检评分
     */
	@DeleteMapping("/{scids}")
    public AjaxResult remove(@PathVariable Long[] scids)
    {
        return toAjax(zjRecordScoreService.deleteZjRecordScoreByScids(scids));
    }


    /**
     * 接收工单分值
     *
     * @param wpSelfSumCount
     * @param returnCountSum
     * @return
     */
    private String getReturnPoint(int wpSelfSumCount, int returnCountSum) {
        if (wpSelfSumCount == 0) {
            return "0.00";
        }
        double v = ((double) returnCountSum / (double) wpSelfSumCount) * 100;
        return String.format("%.2f", v);
    }
}
